package com.audaque.springboot.foshanupload.web.httpclientdemo.ieg.product;

import cn.hutool.http.HttpRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.audaque.springboot.foshanupload.web.exception.RException;
import com.audaque.springboot.foshanupload.web.httpclientdemo.ieg.product.model.IegProduct;
import com.audaque.springboot.foshanupload.web.httpclientdemo.ieg.token.IegTokenProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class IegProductProvider {

    @Autowired
    private IegProductProperties iegProductProperties;

    @Autowired
    private IegTokenProvider iegTokenProvider;

    @Autowired
    private ObjectMapper objectMapper;

    public IegProduct getOne(String id) throws Exception {
        return getOne(id, false);
    }

    private IegProduct getOne(String id, Boolean retryedYn) throws Exception {
        String tokenKey = iegProductProperties.getTokenKey();
        String url = iegProductProperties.getOneUrl(id);
        String v2Token = iegTokenProvider.getV2Token();
        log.debug("ieg查询产品接口 1000086 调用中");

        String result = null;
        try {
            result = HttpRequest.get(url)
                    .header(tokenKey, v2Token)
                    .execute().body();
        } catch (Exception e) {
            throw new RException("ieg查询产品接口 调用过程中异常： 1000086 %s",e.getMessage());
        }




        if (StringUtils.isBlank(result)) {
            throw new RException("ieg查询产品接口 响应异常：无响应");
        }
        IegProduct rsp = (IegProduct) objectMapper.readerFor(IegProduct.class).readValue(result);
        String code = rsp.getCode();
        if (code != null) {
            if (!retryedYn && !iegTokenProvider.authorize(v2Token)) {
                iegTokenProvider.setV2Token(null);
                getOne(id, true);
            }
            throw new RException("ieg查询产品接口 响应异常：%s", rsp.getMsg());
        }
        return rsp;

    }

    public IegProduct insert(IegProduct req) throws Exception {
        return insert(req, false);
    }

    private IegProduct insert(IegProduct req, Boolean retryedYn) throws Exception {
        String tokenKey = iegProductProperties.getTokenKey();
        String url = iegProductProperties.getInsertUrl();
        String v2Token = iegTokenProvider.getV2Token();
        String body = objectMapper.writeValueAsString(req);
        log.debug("ieg创建产品接口 1000086 调用中");

        String result = null;
        try {
            result = HttpRequest.post(url)
                    .header(tokenKey, v2Token).body(body)
                    .execute().body();
        } catch (Exception e) {
            throw new RException("ieg创建产品接口 调用过程中异常 1000086 %s",e.getMessage());
        }

        if (StringUtils.isBlank(result)) {
            throw new RException("ieg创建产品接口 响应异常 1000086 无响应");
        }
        log.debug("ieg创建产品接口 1000086 result {}" ,result);


        IegProduct rsp = (IegProduct) objectMapper.readerFor(IegProduct.class).readValue(result);
        String code = rsp.getCode();
        if (code != null) {
            if (!retryedYn && !iegTokenProvider.authorize(v2Token)) {
                iegTokenProvider.setV2Token(null);
                insert(req, true);
            }
            throw new RException("ieg创建产品接口 响应异常 1000086 %s", rsp.getMsg());
        }
        return rsp;

    }

    public IegProduct update(IegProduct req) throws Exception {
        return update(req, false);
    }

    private IegProduct update(IegProduct req, Boolean retryedYn) throws Exception {
        String tokenKey = iegProductProperties.getTokenKey();
        String url = iegProductProperties.getUpdateUrl(req.getId());
        String v2Token = iegTokenProvider.getV2Token();
        String body = objectMapper.writeValueAsString(req);
        log.debug("ieg更新产品接口 1000086 调用中");

        String result = null;
        try {
            result = HttpRequest.put(url)
                    .header(tokenKey, v2Token).body(body)
                    .execute().body();
        } catch (Exception e) {
            throw new RException("ieg更新产品接口 调用过程中异常 1000086 %s",e.getMessage());
        }

        if (StringUtils.isBlank(result)) {
            throw new RException("ieg更新产品接口 响应异常 1000086 无响应");
        }
        log.debug("ieg更新产品接口 1000086 result {}" ,result);
        IegProduct rsp = null;
        try {
            rsp = (IegProduct) objectMapper.readerFor(IegProduct.class).readValue(result);
        } catch (Exception e) {
            throw new RException("ieg更新产品接口 反序列化异常 1000086 %s", e.getMessage());
        }
        if (rsp!=null&&rsp.getCode() != null) {
            if (!retryedYn && !iegTokenProvider.authorize(v2Token)) {
                iegTokenProvider.setV2Token(null);
                update(req, true);
            }
            throw new RException("ieg更新产品接口 响应异常 1000086 %s", rsp.getMsg());
        }
        return rsp;

    }


}
